home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 March / EnigmA AMIGA RUN 05 (1996)(G.R. Edizioni)(IT)[!][issue 1996-03][Skylink CD IV].iso / earcd / program / ixemlsrc.lha / ixemul / library / __open.c < prev    next >
C/C++ Source or Header  |  1995-12-23  |  4KB  |  134 lines

  1. /*
  2.  *  This file is part of ixemul.library for the Amiga.
  3.  *  Copyright (C) 1991, 1992  Markus M. Wild
  4.  *
  5.  *  This library is free software; you can redistribute it and/or
  6.  *  modify it under the terms of the GNU Library General Public
  7.  *  License as published by the Free Software Foundation; either
  8.  *  version 2 of the License, or (at your option) any later version.
  9.  *
  10.  *  This library is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  *  Library General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU Library General Public
  16.  *  License along with this library; if not, write to the Free
  17.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  *  __open.c,v 1.1.1.1 1994/04/04 04:30:11 amiga Exp
  20.  *
  21.  *  __open.c,v
  22.  * Revision 1.1.1.1  1994/04/04  04:30:11  amiga
  23.  * Initial CVS check in.
  24.  *
  25.  *  Revision 1.3  1992/08/09  20:38:07  amiga
  26.  *  change to use 2.x header files by default
  27.  *
  28.  *  Revision 1.2  1992/07/28  02:51:41  mwild
  29.  *  call AllocDosObject instead of rolling my own when > 1.3
  30.  *
  31.  *  Revision 1.1  1992/05/14  19:55:40  mwild
  32.  *  Initial revision
  33.  *
  34.  */
  35.  
  36. #define KERNEL
  37. #include <string.h>
  38. #include "ixemul.h"
  39.  
  40. #include <utility/tagitem.h>
  41. #include <dos/dostags.h>
  42.  
  43. struct open_vec {
  44.   int action;
  45.   struct FileHandle *fh;
  46. };
  47.  
  48. static int
  49. __open_func (struct StandardPacket *sp, struct MsgPort *handler,
  50.              BPTR parent_lock,
  51.          BSTR name,
  52.          struct open_vec *ov, int *no_error)
  53. {
  54.   sp->sp_Pkt.dp_Type = ov->action;
  55.   sp->sp_Pkt.dp_Arg1 = CTOBPTR (ov->fh);
  56.   sp->sp_Pkt.dp_Arg2 = parent_lock;
  57.   sp->sp_Pkt.dp_Arg3 = name;
  58.  
  59.   PutPacket (handler, sp);
  60.   __wait_sync_packet (sp);
  61.  
  62.   *no_error = sp->sp_Pkt.dp_Res1 == -1;
  63.  
  64.   /* handler is supposed to supply this, but if it didn't... */
  65.   if (*no_error && !ov->fh->fh_Type) ov->fh->fh_Type = handler;
  66.  
  67.   /* continue if we failed because of symlink - reference */
  68.   return 1;
  69. }
  70.  
  71. BPTR
  72. __open (char *name, int action)
  73. {
  74.   unsigned long *fh;
  75.   struct open_vec ov;
  76.   BPTR res;
  77.   struct Process *this_proc = NULL;
  78.   APTR oldwin = NULL;
  79.   char buf[32], *s, *p = buf;
  80.  
  81.   if ((name[0] == '/' && strnicmp(name, "/dev/", 5)) ||
  82.       (index(name, ':') && strnicmp(name, "dev:", 4)))
  83.   {
  84.     s = (name[0] == '/') ? name + 1 : name;
  85.     while (*s && *s != '/' && *s != ':')
  86.       *p++ = *s++;
  87.     *p++ = ':';
  88.     *p = '\0';
  89.     if (ix.ix_flags & ix_no_insert_disk_requester)
  90.     {
  91.       this_proc = (struct Process *)FindTask (0);
  92.       oldwin = this_proc->pr_WindowPtr;
  93.       this_proc->pr_WindowPtr = (APTR)-1;
  94.     }
  95.     if (!IsFileSystem(buf))     /* if NAME is not a file system but */
  96.                                 /* something like a pipe or console, then */
  97.                                 /* do NOT use __plock() but call Open directly */
  98.     {
  99.       if (!*s)
  100.         res = Open(buf, action);
  101.       else if (*s == '/')
  102.       {
  103.         *s = ':';
  104.         res = Open(name + 1, action);
  105.         *s = '/';
  106.       }
  107.       else
  108.         res = Open(name, action);
  109.       if (ix.ix_flags & ix_no_insert_disk_requester)
  110.         this_proc->pr_WindowPtr = oldwin;
  111.       return res;
  112.     }
  113.     if (ix.ix_flags & ix_no_insert_disk_requester)
  114.       this_proc->pr_WindowPtr = oldwin;
  115.   }
  116.   
  117.   /* needed to get berserk ReadArgs() to behave sane, GRRR */
  118.   fh = AllocDosObjectTags (DOS_FILEHANDLE, ADO_FH_Mode, action, TAG_DONE);
  119.   if (! fh)
  120.     return 0;
  121.   ov.fh = (struct FileHandle *) fh;
  122.   ov.action = action;
  123.  
  124.   res = __plock (name, __open_func, &ov);
  125.   /* check for opening of NIL:, in which case we just return
  126.      a filehandle with a zero handle field */
  127.   if (! res && IoErr() == 4242)
  128.     res = -1;
  129.   
  130.   if (! res)
  131.     FreeDosObject (DOS_FILEHANDLE, fh);
  132.   return res ? CTOBPTR(ov.fh) : 0;
  133. }
  134.